// JavaScript Document
function Login(url) {
    var self = this;
    var AddAntiForgeryToken = function (data) {
        data.__RequestVerificationToken = $('#admin_login_form input[name=__RequestVerificationToken]').val();
        return data;
    };
    this.check = function () {
        if (!this.check_UserName()) {
            this.showError('请输入用户名!');
            return false;
        }
        if (!this.check_Password()) {
            this.showError('请输入密码!');
            return false;
        }
        //if (!this.check_code()) {
        //    return false;
        //}
        login.submit();
        return false;
    }
    this.submit = function () {
        var title = $('#login_title').text();
        $('#login_title').text('系统登陆中...');
        $('#admin_login_form').fadeOut(500);
        $('.login-wrapper').addClass('form-success');
        $.ajax({
            url: '/' + url + "/login/submit", type: 'POST', data: AddAntiForgeryToken({ username: $('#username').val(), pass: $('#password').val() }),
            success: function (result) {
                if (result.succ) {
                    location.href = result.data;
                } else {
                    $('#login_title').text(title);
                    $('#admin_login_form').fadeIn(500);
                    self.showError(result.data);
                    $('.login-wrapper').removeClass('form-success');
                }
            }, complete: function () {
                $('#loading').hide();
            }
        });
        return false;
	}
	this.check_UserName = function () {
		var repName = /^[0-9a-zA-Z_.\-@]{3,30}$/;	//检查用户名
		if (!repName.test($('#username').val()) ){
			this.showError("用户名至少为3个字符!");
			return false;
		}else {
			this.hideError();
			return true;
		}
	}
	this.check_Password = function () {
	    var repPass = /^[\S]{3,80}$/;	//检查密码
		if (!repPass.test($('#password').val()) ){
			this.showError("密码至少3-80个字符!");
			return false;
		}else {
			this.hideError();
			return true;
		}
	}
	this.check_code = function () {
	    var repCode = /^[0-9a-zA-Z]{4,6}$/; // 检查验证码
	    if (!repCode.test($('#codestr').val())) {
	        this.showError("验证码输入有误!");
	        return false;
	    } else {
	        this.hideError();
	        return true;
	    }
	}
    this.showError = function (msg) {
        $('#login_tips').text(msg).fadeIn(500);
	}
	this.hideError = function () {
        if ($('#login_tips').is(':visible')) $('#login_tips').fadeOut(500);
	}
	this.getCode = function() {
		var date = new Date();
		$('#ImgCode').attr('src', '/ImageCode.ashx?id=' + date.getSeconds());
		//$('#codestr').attr('value') = "";
	}
}
